#include "game_manager.h"
#include <string>
#include <chrono>
#include <thread>
#include <fstream>

GameManager::GameManager()
{
    this->mWelcomeWindows.resize(2);
    for (int i = 0; i < this->mWelcomeWindows.size(); i++)
    {
        this->mWelcomeWindows[i] = nullptr;
    }
    initscr();
    nodelay(stdscr, true);
    keypad(stdscr, true);
    noecho();
    curs_set(0);

    getmaxyx(stdscr, mScreenHeight, mScreenWidth);
    mControlPanelWidth = mScreenWidth * 0.4;
    mInstructionPanelWidth = mScreenWidth - mControlPanelWidth;

    this->createWelcomeWindows();
    this->loadSkinData();
}

GameManager::~GameManager()
{
    for (int i = 0; i < this->mWelcomeWindows.size(); i++)
    {
        delwin(this->mWelcomeWindows[i]);
    }
    endwin();
}

void GameManager::createWelcomeWindows()
{
    int startY = 0;
    int startX = 0;
    this->mWelcomeWindows[0] = newwin(this->mScreenHeight, this->mControlPanelWidth, startY, startX);
    box(this->mWelcomeWindows[0], 0, 0);
    startX = this->mControlPanelWidth;
    this->mWelcomeWindows[1] = newwin(this->mScreenHeight, this->mInstructionPanelWidth, startY, startX);
    box(this->mWelcomeWindows[1], 0, 0);
}

void GameManager::renderWelcomeWindows() const
{
    this->renderControlPanel();
    this->renderInstructionPanel();
}

void GameManager::renderControlPanel() const
{
    wclear(this->mWelcomeWindows[0]);
    box(this->mWelcomeWindows[0], 0, 0);

    mvwprintw(this->mWelcomeWindows[0], 1, 1, "Welcome to The Snake Game!");
    mvwprintw(this->mWelcomeWindows[0], 2, 1, "Enhanced Version with Various Features");
    mvwprintw(this->mWelcomeWindows[0], 3, 1, "Authors: Jiang Yiyang, Mei Zhaoguo, Wang Lianwen");
    mvwprintw(this->mWelcomeWindows[0], 4, 1, "Website: https://gitee.com/jyy16/snakegame");

    mvwprintw(this->mWelcomeWindows[0], 6, 1, "Menu Controls:");
    mvwprintw(this->mWelcomeWindows[0], 7, 1, "Up: W or Arrow Key");
    mvwprintw(this->mWelcomeWindows[0], 8, 1, "Down: S or Arrow Key");
    mvwprintw(this->mWelcomeWindows[0], 9, 1, "Select: Space or Enter");
    mvwprintw(this->mWelcomeWindows[0], 10, 1, "Quit: Q");

    wrefresh(this->mWelcomeWindows[0]);
}

void GameManager::renderInstructionPanel() const
{
    wclear(this->mWelcomeWindows[1]);
    box(this->mWelcomeWindows[1], 0, 0);

    mvwprintw(this->mWelcomeWindows[1], 1, 1, "Game Instructions");

    mvwprintw(this->mWelcomeWindows[1], 3, 1, "Objective:");
    mvwprintw(this->mWelcomeWindows[1], 4, 1, "- Control snake to eat food");
    mvwprintw(this->mWelcomeWindows[1], 5, 1, "- Avoid walls, obstacles and snake body");

    mvwprintw(this->mWelcomeWindows[1], 7, 1,  "Player 1:                     Player 2:");
    mvwprintw(this->mWelcomeWindows[1], 8, 1,  "Up: W                         Up: I");
    mvwprintw(this->mWelcomeWindows[1], 9, 1,  "Down: S                       Down: K");
    mvwprintw(this->mWelcomeWindows[1], 10, 1, "Left: A                       Left: J");
    mvwprintw(this->mWelcomeWindows[1], 11, 1, "Right: D                      Right: L");
    mvwprintw(this->mWelcomeWindows[1], 12, 1, "Skill: J(Single)/E(Double)    Skill: O");

    mvwprintw(this->mWelcomeWindows[1], 14, 1, "Special Features:");
    mvwprintw(this->mWelcomeWindows[1], 15, 1, "- Directional bonus food in all mode");
    mvwprintw(this->mWelcomeWindows[1], 16, 1, "    Extra points for correct direction");
    mvwprintw(this->mWelcomeWindows[1], 18, 1, "- Jump over obstacles in Single mode at the cost of your tail");
    mvwprintw(this->mWelcomeWindows[1], 20, 1, "- Boost mode in Double mode for extra speed");
    mvwprintw(this->mWelcomeWindows[1], 21, 1, "    Press the key of the moving direction to activate.");
    mvwprintw(this->mWelcomeWindows[1], 22, 1, "    Press again or turn to deactivate.");
    mvwprintw(this->mWelcomeWindows[1], 23, 1, "- Special blinking food in Double mode to confuse your opponent.");
    mvwprintw(this->mWelcomeWindows[1], 24, 1, "- Optional FinalFight for Double mode");
    mvwprintw(this->mWelcomeWindows[1], 25, 1, "- Use skill in Double mode to leave obstacles behind");
    mvwprintw(this->mWelcomeWindows[1], 27, 1, "- Fight against fierce computer in PVE mode");

    mvwprintw(this->mWelcomeWindows[1], 29, 1, "Scoring:");
    mvwprintw(this->mWelcomeWindows[1], 30, 1, "- Normal food: +1 point");
    mvwprintw(this->mWelcomeWindows[1], 31, 1, "- Bonus food: +1 point & +2 bonus points");
    mvwprintw(this->mWelcomeWindows[1], 32, 1, "- Difficulty increases with score");
    
    mvwprintw(this->mWelcomeWindows[1], 34, 1, "Game Controls:");
    mvwprintw(this->mWelcomeWindows[1], 35, 1, "- Press P to Pause/Resume");
    
    mvwprintw(this->mWelcomeWindows[1], 37, 1, "Current Skin For Single or PVE mode:");
    mvwprintw(this->mWelcomeWindows[1], 38, 1, "%s", mSkinNames[mCurrentSkin].c_str());

    wrefresh(this->mWelcomeWindows[1]);
}

// 加载皮肤数据
void GameManager::loadSkinData()
{
    // 初始化皮肤名称
    mSkinNames = {"Python", "Pip3", "Anaconda"};
    
    // 从文件中读取皮肤数据
    std::string skinFiles[] = {"skin1.txt", "skin2.txt", "skin3.txt"};
    mSkinData.clear();
    
    for (int i = 0; i < 3; i++) {
        std::vector<std::string> skinData;
        std::ifstream file(skinFiles[i]);
        
        if (file.is_open()) {
            std::string line;
            while (std::getline(file, line)) {
                skinData.push_back(line);
            }
            file.close();
        } else {
            // 如果文件不存在，使用默认数据
            skinData.push_back("Error: Cannot load skin " + std::to_string(i + 1));
        }
        
        mSkinData.push_back(skinData);
    }
}

int GameManager::renderSkinSelectionMenu() const
{
    int selectedOption = 0;
    int lastSelectedOption = -1; // 初始化为-1确保第一次绘制
    const int totalOptions = 4; // 3个皮肤 + 1个返回选项
    
    // 初始化左侧窗口
    wclear(this->mWelcomeWindows[0]);
    box(this->mWelcomeWindows[0], 0, 0);
    
    // 显示固定内容
    mvwprintw(this->mWelcomeWindows[0], 1, 1, "Select Skin:");
    mvwprintw(this->mWelcomeWindows[0], 2, 1, "================");
    mvwprintw(this->mWelcomeWindows[0], 13, 1, "Controls:");
    mvwprintw(this->mWelcomeWindows[0], 14, 1, "W/S: Navigate");
    mvwprintw(this->mWelcomeWindows[0], 15, 1, "Enter: Select");
    mvwprintw(this->mWelcomeWindows[0], 16, 1, "F: Open txt window");
    mvwprintw(this->mWelcomeWindows[0], 17, 1, "Q: Back");
    
    // 初始化右侧窗口
    wclear(this->mWelcomeWindows[1]);
    box(this->mWelcomeWindows[1], 0, 0);
    mvwprintw(this->mWelcomeWindows[1], 1, 1, "Select a skin to preview");
    wrefresh(this->mWelcomeWindows[1]);
    
    while (true)
    {
        // 只在选项改变时重新绘制
        if (selectedOption != lastSelectedOption)
        {
            // 显示皮肤选项
            for (int i = 0; i < 3; i++)
            {
                if (selectedOption == i)
                {
                    wattron(this->mWelcomeWindows[0], A_REVERSE);
                    mvwprintw(this->mWelcomeWindows[0], 4 + i * 2, 2, "> %s", mSkinNames[i].c_str());
                    wattroff(this->mWelcomeWindows[0], A_REVERSE);
                }
                else
                {
                    mvwprintw(this->mWelcomeWindows[0], 4 + i * 2, 2, "  %s", mSkinNames[i].c_str());
                }
            }
            
            // 返回选项
            if (selectedOption == 3)
            {
                wattron(this->mWelcomeWindows[0], A_REVERSE);
                mvwprintw(this->mWelcomeWindows[0], 11, 2, "> Back to Main Menu");
                wattroff(this->mWelcomeWindows[0], A_REVERSE);
            }
            else
            {
                mvwprintw(this->mWelcomeWindows[0], 11, 2, "  Back to Main Menu");
            }
            
            // 只在这里刷新左侧窗口
            wrefresh(this->mWelcomeWindows[0]);
            
            // 显示当前选中皮肤的预览
            if (selectedOption < 3)
            {
                this->renderSkinPreview(selectedOption);
            }
            else if (selectedOption == 3)
            {
                // 清除右侧窗口
                wclear(this->mWelcomeWindows[1]);
                box(this->mWelcomeWindows[1], 0, 0);
                mvwprintw(this->mWelcomeWindows[1], 1, 1, "Select a skin to preview");
                wrefresh(this->mWelcomeWindows[1]);
            }
            
            lastSelectedOption = selectedOption;
        }
        
        // 处理输入
        int key = getch();
        switch (key)
        {
            case 'w':
            case 'W':
            case KEY_UP:
                selectedOption = (selectedOption - 1 + totalOptions) % totalOptions;
                break;
            case 's':
            case 'S':
            case KEY_DOWN:
                selectedOption = (selectedOption + 1) % totalOptions;
                break;
            case '\n':
            case '\r':
            case ' ':
                if (selectedOption < 3)
                {
                    return selectedOption; // 返回选中的皮肤索引
                }
                else
                {
                    return -1; // 返回主菜单
                }
            case 'f':
            case 'F':
                if (selectedOption < 3)
                {
                    this->openTxtWindow(selectedOption);
                    // 重新绘制界面
                    clear();
                    refresh();
                    // 初始化左侧窗口
                    box(this->mWelcomeWindows[0], 0, 0);
                    
                    // 显示固定内容
                    mvwprintw(this->mWelcomeWindows[0], 1, 1, "Select Skin:");
                    mvwprintw(this->mWelcomeWindows[0], 2, 1, "================");
                    mvwprintw(this->mWelcomeWindows[0], 13, 1, "Controls:");
                    mvwprintw(this->mWelcomeWindows[0], 14, 1, "W/S: Navigate");
                    mvwprintw(this->mWelcomeWindows[0], 15, 1, "Enter: Select");
                    mvwprintw(this->mWelcomeWindows[0], 16, 1, "F: Open txt window");
                    mvwprintw(this->mWelcomeWindows[0], 17, 1, "Q: Back");

                    lastSelectedOption = -1; // 强制重绘
                }
                break;
            case 'q':
            case 'Q':
                return -1; // 返回主菜单
        }
        
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}

// 渲染皮肤预览
void GameManager::renderSkinPreview(int skinIndex) const
{
    if (skinIndex < 0 || skinIndex >= mSkinData.size())
    {
        return;
    }
    
    wclear(this->mWelcomeWindows[1]);
    box(this->mWelcomeWindows[1], 0, 0);
    
    mvwprintw(this->mWelcomeWindows[1], 1, 1, "Preview: %s", mSkinNames[skinIndex].c_str());
    mvwprintw(this->mWelcomeWindows[1], 2, 1, "========================");
    
    // 显示皮肤预览（只显示前面几行，避免超出窗口）
    int maxLines = std::min((int)mSkinData[skinIndex].size(), mScreenHeight - 8);
    for (int i = 0; i < maxLines; i++)
    {
        if (i + 4 < mScreenHeight - 2)
        {
            // 截取字符串以适应窗口宽度
            std::string line = mSkinData[skinIndex][i];
            if (line.length() > mInstructionPanelWidth - 4)
            {
                line = line.substr(0, mInstructionPanelWidth - 4);
            }
            mvwprintw(this->mWelcomeWindows[1], i + 4, 1, "%s", line.c_str());
        }
    }
    
    wrefresh(this->mWelcomeWindows[1]);
}

// 获取指定皮肤的数据
const std::vector<std::string>& GameManager::getSkinData(int skinIndex) const
{
    if (skinIndex >= 0 && skinIndex < mSkinData.size())
    {
        return mSkinData[skinIndex];
    }
    return mSkinData[0]; // 默认返回第一个皮肤
}

// 显示完整的皮肤预览窗口
void GameManager::showFullSkinPreview(int skinIndex) const
{
    if (skinIndex < 0 || skinIndex >= mSkinData.size())
    {
        return;
    }
    
    // 创建全屏预览窗口
    WINDOW *previewWindow = newwin(mScreenHeight, mScreenWidth, 0, 0);
    
    while (true)
    {
        wclear(previewWindow);
        box(previewWindow, 0, 0);
        
        // 标题
        mvwprintw(previewWindow, 1, 2, "Full Preview: %s", mSkinNames[skinIndex].c_str());
        mvwprintw(previewWindow, 2, 2, "===============================================");
        
        // 显示皮肤数据
        int displayLines = std::min((int)mSkinData[skinIndex].size(), mScreenHeight - 6);
        for (int i = 0; i < displayLines; i++)
        {
            std::string line = mSkinData[skinIndex][i];
            if (line.length() > mScreenWidth - 4)
            {
                line = line.substr(0, mScreenWidth - 4);
            }
            mvwprintw(previewWindow, i + 4, 2, "%s", line.c_str());
        }
        
        // 控制说明
        mvwprintw(previewWindow, mScreenHeight - 2, 2, "Press ESC or Q to return to skin selection");
        
        wrefresh(previewWindow);
        
        // 处理输入
        int key = getch();
        if (key == 27 || key == 'q' || key == 'Q') // ESC键或Q键
        {
            break;
        }
        
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    
    delwin(previewWindow);
}

// 打开txt窗口 - 使用类似PvE中showRuleFile的方法
void GameManager::openTxtWindow(int skinIndex) const
{
    if (skinIndex < 0 || skinIndex >= mSkinData.size())
    {
        return;
    }
    
    // 创建皮肤显示窗口
    int dialogHeight = this->mScreenHeight - 4;
    int dialogWidth = this->mScreenWidth - 4;
    int startY = 2;
    int startX = 2;
    
    WINDOW* skinWin = newwin(dialogHeight, dialogWidth, startY, startX);
    keypad(skinWin, TRUE);
    
    // 准备皮肤数据
    std::vector<std::string> skinLines;
    skinLines.push_back("========== " + mSkinNames[skinIndex] + " Skin Content ==========");
    skinLines.push_back("");
    
    for (const auto& line : mSkinData[skinIndex])
    {
        skinLines.push_back(line);
    }
    
    skinLines.push_back("");
    skinLines.push_back("========== End of " + mSkinNames[skinIndex] + " Skin ==========");
    
    int scrollOffset = 0;
    int maxDisplayLines = dialogHeight - 4; // 减去边框和标题行
    int key;
    
    while (true)
    {
        wclear(skinWin);
        box(skinWin, 0, 0);
        
        // 标题
        std::string title = mSkinNames[skinIndex] + " Skin Preview";
        mvwprintw(skinWin, 1, (dialogWidth - title.length()) / 2, "%s", title.c_str());
        
        // 显示皮肤内容
        int displayLine = 2;
        for (int i = scrollOffset; i < static_cast<int>(skinLines.size()) && displayLine < dialogHeight - 2; i++)
        {
            // 处理长行，如果超过窗口宽度则截断
            std::string line = skinLines[i];
            if (line.length() > static_cast<size_t>(dialogWidth - 4))
            {
                line = line.substr(0, dialogWidth - 4);
            }
            mvwprintw(skinWin, displayLine, 2, "%s", line.c_str());
            displayLine++;
        }
        
        // 底部控制提示
        mvwprintw(skinWin, dialogHeight - 2, 2, "Controls: W/S or Arrow Keys to scroll, Q to close");
        
        // 如果有更多内容可以滚动，显示滚动指示器
        if (scrollOffset > 0)
        {
            mvwprintw(skinWin, 2, dialogWidth - 3, "^");
        }
        if (scrollOffset + maxDisplayLines < static_cast<int>(skinLines.size()))
        {
            mvwprintw(skinWin, dialogHeight - 3, dialogWidth - 3, "v");
        }
        
        wrefresh(skinWin);
        
        key = wgetch(skinWin);
        
        switch (key)
        {
            case 'w':
            case 'W':
            case KEY_UP:
                if (scrollOffset > 0)
                    scrollOffset--;
                break;
            case 's':
            case 'S':
            case KEY_DOWN:
                if (scrollOffset + maxDisplayLines < static_cast<int>(skinLines.size()))
                    scrollOffset++;
                break;
            case 'q':
            case 'Q':
                delwin(skinWin);
                return;
            default:
                break;
        }
    }
}

int GameManager::renderWelcomeMenu() const
{
    this->renderWelcomeWindows();
    refresh();

    WINDOW *menu;
    int width = this->mControlPanelWidth * 0.8;
    int height = 10;
    int startX = this->mControlPanelWidth * 0.1;
    int startY = this->mScreenHeight * 0.6;

    menu = newwin(height, width, startY, startX);
    box(menu, 0, 0);
    std::vector<std::string> menuItems = {"Single Player", "Double Players", "PvE Mode", "Select Skin", "Quit"};

    int index = 0;
    int offset = 3;
    mvwprintw(menu, 1, 1, "Please select an option:");
    wattron(menu, A_STANDOUT);
    mvwprintw(menu, 0 + offset, 1, menuItems[0].c_str());
    wattroff(menu, A_STANDOUT);
    mvwprintw(menu, 1 + offset, 1, menuItems[1].c_str());
    mvwprintw(menu, 2 + offset, 1, menuItems[2].c_str());
    mvwprintw(menu, 3 + offset, 1, menuItems[3].c_str());
    mvwprintw(menu, 4 + offset, 1, menuItems[4].c_str());

    wrefresh(menu);

    int key;
    while (true)
    {
        key = getch();
        switch (key)
        {
        case 'W':
        case 'w':
        case KEY_UP:
        {
            mvwprintw(menu, index + offset, 1, menuItems[index].c_str());
            index--;
            index = (index < 0) ? menuItems.size() - 1 : index;
            wattron(menu, A_STANDOUT);
            mvwprintw(menu, index + offset, 1, menuItems[index].c_str());
            wattroff(menu, A_STANDOUT);
            break;
        }
        case 'S':
        case 's':
        case KEY_DOWN:
        {
            mvwprintw(menu, index + offset, 1, menuItems[index].c_str());
            index++;
            index = (index > menuItems.size() - 1) ? 0 : index;
            wattron(menu, A_STANDOUT);
            mvwprintw(menu, index + offset, 1, menuItems[index].c_str());
            wattroff(menu, A_STANDOUT);
            break;
        }
        case 'q':
        case 'Q':
        {
            delwin(menu);
            return -1;
        }
        }
        wrefresh(menu);
        if (key == ' ' || key == 10)
        {
            break;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    delwin(menu);

    return index;
}

void GameManager::startWelcomeScreen()
{
    while (true)
    {
        // 清理之前的窗口
        for (int i = 0; i < this->mWelcomeWindows.size(); i++)
        {
            if (this->mWelcomeWindows[i] != nullptr)
            {
                delwin(this->mWelcomeWindows[i]);
                this->mWelcomeWindows[i] = nullptr;
            }
        }
        
        // 重新创建欢迎窗口
        this->createWelcomeWindows();
        this->renderWelcomeWindows();
        refresh();

        int startGame = this->renderWelcomeMenu();
        
        if (startGame == -1)
        {
            return; // 退出
        }

        switch (startGame)
        {
        case 0:
        {
            // 清理欢迎窗口
            for (int i = 0; i < this->mWelcomeWindows.size(); i++)
            {
                delwin(this->mWelcomeWindows[i]);
                this->mWelcomeWindows[i] = nullptr;
            }
            
            Game game(mCurrentSkin);  // 传递当前选择的皮肤索引
            game.startGame();
            
            // 游戏结束后重新初始化屏幕
            clear();
            refresh();
            break;
        }
        case 1:
        {
            // 清理欢迎窗口
            for (int i = 0; i < this->mWelcomeWindows.size(); i++)
            {
                delwin(this->mWelcomeWindows[i]);
                this->mWelcomeWindows[i] = nullptr;
            }

            clear();
            refresh();

            // 询问是否启用鏖战
            bool enableFinalFight = this->askForFinalFight();
            
            doubleGame game(enableFinalFight);  // 传递鏖战设置
            game.startGame();
            
            // 游戏结束后重新初始化屏幕
            clear();
            refresh();
            break;
        }
        case 2:
        {
            // PvE模式
            // 清理欢迎窗口
            for (int i = 0; i < this->mWelcomeWindows.size(); i++)
            {
                delwin(this->mWelcomeWindows[i]);
                this->mWelcomeWindows[i] = nullptr;
            }
            
            clear();
            refresh();

            // 创建PvE游戏实例
            pveGame game(mCurrentSkin);
            game.startGame();
            
            // 游戏结束后重新初始化屏幕
            clear();
            refresh();
            break;
        }
        case 3:
        {
            // 皮肤选择
            int selectedSkin = this->renderSkinSelectionMenu();
            if (selectedSkin >= 0)
            {
                mCurrentSkin = selectedSkin;
                // 显示皮肤选择成功信息
                wclear(this->mWelcomeWindows[0]);
                box(this->mWelcomeWindows[0], 0, 0);
                mvwprintw(this->mWelcomeWindows[0], 1, 1, "Skin Selected:");
                mvwprintw(this->mWelcomeWindows[0], 2, 1, "%s", mSkinNames[selectedSkin].c_str());
                mvwprintw(this->mWelcomeWindows[0], 4, 1, "Press any key to continue...");
                wrefresh(this->mWelcomeWindows[0]);
                getch();
            }
            break;
        }
        case 4:
        {
            return;
        }
        }
    }
}

// 询问是否启用鏖战
bool GameManager::askForFinalFight() const
{
    // 创建询问窗口
    int width = 60;
    int height = 12;
    int startX = (mScreenWidth - width) / 2;
    int startY = (mScreenHeight - height) / 2;
    
    WINDOW* askWindow = newwin(height, width, startY, startX);
    box(askWindow, 0, 0);
    // Display inquiry message
    mvwprintw(askWindow, 1, 2, "Two Player Mode Final Fight Settings");
    mvwprintw(askWindow, 2, 2, "========================");
    mvwprintw(askWindow, 3, 2, "Final Fight: After 45 seconds, enter Final Fight mode");
    mvwprintw(askWindow, 4, 2, "Effect: Display FINAL FIGHT and 3x speed");
    mvwprintw(askWindow, 5, 2, "");
    mvwprintw(askWindow, 6, 2, "Enable Final Fight mode?");
    mvwprintw(askWindow, 7, 2, "Y - Enable  N - Disable (Default: Disabled)");
    mvwprintw(askWindow, 8, 2, "Please select: ");
    
    wrefresh(askWindow);
    
    // 临时禁用nodelay，确保getch()会等待用户输入
    nodelay(stdscr, false);
    cbreak();  // 确保输入模式正确
    
    bool finalFightEnabled = false;
    int key;
    
    while (true)
    {
        key = wgetch(askWindow);  // 使用窗口的getch
        
        switch (key)
        {
            case 'y':
            case 'Y':
                finalFightEnabled = true;
                mvwprintw(askWindow, 9, 2, "Final Fight On!");
                wrefresh(askWindow);
                std::this_thread::sleep_for(std::chrono::milliseconds(1000));
                break;
                
            case 'n':
            case 'N':
            case '\n':
            case '\r':
            case 27:  // ESC键
                finalFightEnabled = false;
                mvwprintw(askWindow, 9, 2, "Final Fight Off!");
                wrefresh(askWindow);
                std::this_thread::sleep_for(std::chrono::milliseconds(1000));
                break;
                
            default:
                // 忽略其他按键，继续等待
                continue;
        }
        
        // 有效选择后退出循环
        break;
    }
    
    // 清理窗口
    delwin(askWindow);
    
    // 恢复nodelay设置
    nodelay(stdscr, true);
    
    // 清屏并刷新
    clear();
    refresh();
    
    return finalFightEnabled;
}